<template>
  <div class="home">
    <!-- 顶部导航 -->
    <van-nav-bar title="首页" left-text="返回" fixed />
    <!-- 轮播图 -->
    <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
      <van-swipe-item
        :style="{ backgroundColor: '#999', height: '150px' }"
        v-for="(item, index) in banners"
        :key="index"
        ><img :src="item.pic" alt="" width="100%"
      /></van-swipe-item>
    </van-swipe>
    <!-- 区域标题 -->
    <div class="section-title">
      <span class="line"> </span>
      编辑推荐
    </div>
    <!-- 歌单 -->
    <div class="music-list">
      <div class="items" v-for="item in result" :key="item.id">
        <div class="content">
          <img :src="item.picUrl" alt="" />
          <p>{{ item.name }}</p>
        </div>
      </div>
    </div>

    <!-- 区域标题 -->
    <div class="section-title">
      <span class="line"> </span>
      最新音乐
    </div>
    <!-- 列表 -->
    <div class="song-list">
      <van-cell
        v-for="item in songList"
        :key="item.id"
        :title="item.name"
        size="large"
        :label="item.song.artists[0].name"
        @click="$router.push({ path: '/play', query: { id: item.id } })"
      >
        <template #right-icon>
          <van-icon name="play-circle-o" class="play-icon" />
        </template>
      </van-cell>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Home',
  data () {
    return {
      banners: [],
      result: [],
      songList: []
    }
  },
  created () {
    // 发送ajax请求获得数据
    this.$axios({
      url: '/banner?type=2',
      method: 'get'
    }).then(res => {
      this.banners = res.data.banners
    })

    //请求的是推荐列表
    this.$axios({
      url: '/personalized?limit=12',
      method: 'get'
    }).then(res => {
      this.result = res.data.result
    })

    //请求歌单列表
    this.$axios({
      url: '/personalized/newsong',
      method: 'get'
    }).then(res => {
      // console.log(res)
      this.songList = res.data.result
    })
  }
}
</script>
<style lang="less">
.home {
  padding-top: 46px;
}
.section-title {
  height: 50px;
  position: relative;
  background: #fcfcfd;
  border-bottom: 1px solid #e6e6e6;
  line-height: 50px;
  color: gray;
  padding-left: 10px;
  .line {
    width: 4px;
    background-color: #d43c33;
    height: 20px;
    left: 0;
    top: 15px;
    position: absolute;
  }
}
.swipe-img {
  width: 100%;
}
.music-list {
  display: flex;
  flex-wrap: wrap;
  .items {
    box-sizing: border-box;
    width: 33.333%;
    padding: 0 2px;
    margin-bottom: 10px;
    img {
      display: block;
      width: 100%;
    }
    p {
      padding: 0;
      margin: 0;
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 2;
      padding: 2px;
      font-size: 14px;
      color: gray;
    }
  }
}
.song-list .play-icon {
  font-size: 30px;
  color: gray;
}
.song-list {
  .van-cell__title {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}
</style>
